//
//  XDLMSanPostInfoViewController.m
//  xindailianmeng
//
//  Created by 许佳奇 on 17/2/10.
//  Copyright © 2017年 xindailianmeng.cn. All rights reserved.
//*******散标详情

#import "XDLMSanPostInfoViewController.h"
#import "XDLMSanPostTableViewCell.h"
#import "AFHTTPSessionManager+POST.h"
#import "XDLMUIFactory.h"
#import "XDLMSanPostTwoTableViewCell.h"

#import "XDLMSanPostThreeTableViewCell.h"
#import "XDLMSanPostInfoModel.h"
#import "ZHRTBasicMethod.h"
#import "XDLMImmediateInvestmentViewController.h"
#define TEXTCOLOR [UIColor colorWithRed:51.0/255.0 green:51.0/255.0 blue:51.0/255.0 alpha:1]
@interface XDLMSanPostInfoViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic, strong)NSMutableArray *dataSource;
@property (nonatomic, assign)BOOL isSelected;//cell是否选中
@property (nonatomic, assign)NSInteger selectRow;//选中哪一个row
@property (nonatomic, assign)NSInteger lastRow;//上一个选中的row
@property (nonatomic, strong)XDLMSanPostInfoModel *model;

@property (nonatomic, strong)NSMutableArray *problemArray;
@property (nonatomic, strong)NSMutableArray *infoArray;
@property (nonatomic, copy)NSString *state;


@end
static NSString * one = @"XDLMSanPostTableViewCell";
static NSString * two = @"XDLMSanPostTwoTableViewCell";
static NSString * three = @"XDLMSanPostThreeTableViewCell";

@implementation XDLMSanPostInfoViewController
{
    UITableView *_table;
    UILabel *_numberLab;//编号Lab
    UILabel *_persentLab;//利率Lab
    UILabel *_expectLab;//预期年华Lab
    UILabel *_monthLab;//月份Lab
    
    UILabel *_limitLab;//
    UILabel *_moneyLab;//金额Lab;
    UILabel *_leftLab;//剩余磕头金额Lab
    UILabel *_dateOneLab;//进程时间1
    UILabel *_dateTwoLab;//进程时间2Lab
    
    UILabel *_dateThreeLab;//进程时间3
    UILabel *_totalLab;//总额Lab
    UILabel *_payBackLab;//还款方式Lab
    UILabel *_companyLab;//担保机构Lab
    UIImageView *_ImgOne;
    
    UIImageView *_ImgTwo;
    NSArray *_titleArray;
    
    NSArray *_dataArray;
    NSDictionary *_dataDic;
    
    NSMutableArray *_selectArray;
    NSArray *textArray;
    
    UIButton *_calculatorBtn;//计算器
    UIButton *_startBuy;//立即抢购
    UIView *_maskView;//灰色蒙版
    UIView *_calculatorBackView;//弹出计算器view
    UILabel *_newIncomeLab;//最新收益lab
    
    UILabel *_calPercentLab;
    UILabel *_calMonthLab;
    UITextField *_calTF;//计算器输入框
    
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self loadData];
    [self.view setBackgroundColor:[UIColor whiteColor]];
    _selectArray = [[NSMutableArray alloc] initWithObjects:@"",@"1",@"",@"1",@"",@"1",@"", nil];
    textArray = @[@"到了快解放拉三等奖富家大室了放假了了速度加福禄寿安静了分开都计算量看风景是锻炼放开就睡了肯定就弗兰克的时间弗兰克圣诞节 福利局三发福利家具啥了就撒代理费胜利大街分",@"的方式来看极度分裂静安寺六块腹肌陆金所流口水就捡垃圾抗裂砂浆的减肥法拉三等奖陆金所到了睡了溜达林姐",@"的放辣椒是六角螺栓的陆金所林姐的三六九等福利林姐大家快来分手来了捡垃圾就 发来得及来得及"];
    _dataArray =@[@"dfdsafdsfdsfasdf",@"fdsafasdfasfasdf"];
    _titleArray = @[@"",@"平台资质认证",@"",@"项目简介",@"",@"投资记录"];
    _dataDic = [NSDictionary dictionaryWithObjectsAndKeys:@"fdfdd饭拉倒会计分录空间登录到家了可放假了肯定撒林姐就 捡垃圾林姐林姐",@"1",@"发电量会计分录是接待来访加速度房间里的是街坊邻居",@"2",@" 打开就福利局啊裸考军队家里贷款了解多少放辣椒是矮了",@"3", nil];
    
    self.navigationItem.title = @"项目介绍";
    [self.navigationController.navigationBar setBarStyle:UIBarStyleDefault];
    [self createRightItem];
    // Do any additional setup after loading the view.
}
- (void)loadData
{
    if (self.recordID == nil) {
        self.recordID = @"0";
    }
    NSDictionary *param = @{@"method":@"toBuyMultiPost",@"postId":@"35"};
    [AFHTTPSessionManager POSTWithParams:param success:^(NSURLSessionDataTask *task, id responseObject)
     {
         NSDictionary *dic = responseObject[@"result"];
         if ([responseObject[@"code"] isEqualToString:@"100000"]) {
             
             self.model = [[XDLMSanPostInfoModel alloc] initWithDictionary:dic error:nil];
             self.problemArray = responseObject[@"result"][@"postProblems"];
             self.infoArray = responseObject[@"result"][@"problemIntroductions"];
             self.dataSource = responseObject[@"result"][@"investRecordList"];
             self.state = [NSString stringWithFormat:@"%@",responseObject[@"result"][@"status"]];
             NSLog(@"responseObject==%@",responseObject);
             NSLog(@"%@",self.dataSource);
            
             [self addViews];
             [_table reloadData];
         }
     } failure:^(NSURLSessionDataTask *task, NSError *error)
     {
         
     }];
}
- (void)addViews
{
    
    _table = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kScreenW, kScreenH - 64*kscale-50) style:UITableViewStylePlain];
    [_table setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    _table.delegate = self;
    _table.dataSource = self;
    _table.tableHeaderView = [self setTopView];
    _table.showsVerticalScrollIndicator = NO;
    [self.view addSubview:_table];
    
    
    if ([self.state isEqualToString:@"0"]) {
        
    }
    else if ([self.state isEqualToString:@"1"]){
    
    }
    else if ([self.state isEqualToString:@"2"]){
        
    }
    else if ([self.state isEqualToString:@"3"]){
        
    }
    else if ([self.state isEqualToString:@"4"]){
        
    }
    UIView *bottomView = [[UIView alloc] initWithFrame:CGRectMake(0, kScreenH - 50*kscale-64, kScreenW, 50*kscale)];
    bottomView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:bottomView];
    UILabel *lineOneLab = [self createLabelWithFrame:CGRectMake(0, 0, kScreenW, 0.5) text:@"" textFont:nil textColor:nil];
    lineOneLab.backgroundColor = [UIColor colorWithRed:204.0/255.0 green:204.0/255.0 blue:204.0/255.0 alpha:204.0/255.0];
    [bottomView addSubview:lineOneLab];
    _calculatorBtn = [[UIButton alloc] initWithFrame:CGRectMake(15, 8*kscale, 32*kscale, 32*kscale)];
    [_calculatorBtn setBackgroundImage:[UIImage imageNamed:@"jisuanqi"] forState:UIControlStateNormal];
    [_calculatorBtn addTarget:self action:@selector(calculatorBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [bottomView addSubview:_calculatorBtn];
    
    _startBuy = [[UIButton alloc] initWithFrame:CGRectMake(67*kscale, 8*kscale, kScreenW-67*kscale-15, 32*kscale)];
    [_startBuy setBackgroundImage:[UIImage imageNamed:@"startinvest"] forState:UIControlStateNormal];
    [_startBuy addTarget:self action:@selector(startBuyClick) forControlEvents:UIControlEventTouchUpInside];
    [bottomView addSubview:_startBuy];
    
}
#pragma mark -- UITableViewDataSource && Delegate
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    if (indexPath.row == 1) {
        XDLMSanPostTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:one];
        if (!cell) {
            cell = [[XDLMSanPostTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier :one];
            [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
        }
        cell.title = [_titleArray objectAtIndex:indexPath.row];
        [cell reloadData];
        return cell;
    }
    else if (indexPath.row == 3){
        XDLMSanPostTwoTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:two];
        if (!cell) {
            cell = [[XDLMSanPostTwoTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:two];
            [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
        }
       
        [cell setIsSelect:^(BOOL select) {
            if (select) {
                
                [_selectArray replaceObjectAtIndex:indexPath.row withObject:@"2"];
                
            }
            else
            {
                [_selectArray replaceObjectAtIndex:indexPath.row withObject:@"1"];
            }
            [tableView reloadData];
            
        }];
        cell.titleArray = self.problemArray;
        cell.title = [_titleArray objectAtIndex:indexPath.row];
        [cell reloadData:_infoArray];
        return cell;
    }
    else if(indexPath.row == 5){
        XDLMSanPostThreeTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:three];
        if (!cell) {
            cell = [[XDLMSanPostThreeTableViewCell alloc ] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:three];
            [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
        }
        [cell setIsSelsct:^(BOOL select) {
            if (select) {
                [_selectArray replaceObjectAtIndex:indexPath.row withObject:@"2"];
            }
            else
            {
                [_selectArray replaceObjectAtIndex:indexPath.row withObject:@"1"];
            }
            
            [tableView reloadData];
        }];
        cell.title = [_titleArray objectAtIndex:indexPath.row];
        [cell reloadData:self.dataSource];
        return cell;
    }
    else
    {
        if (indexPath.row == 6) {
            UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"line"];
            if (!cell) {
                cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"line"];
                [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
                UILabel *line = [[UILabel alloc] initWithFrame:CGRectMake(0,0 , kScreenW, 0.5)];
                [line setBackgroundColor:[UIColor colorWithRed:102.0/255.0 green:102.0/255.0 blue:102.0/255.0 alpha:1]];
                [cell.contentView addSubview:line];
            }
            return cell;
        }
        else
        {
            UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"line1"];
            if (!cell) {
                cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"line1"];
                [cell addSubview:[self lineView]];
                [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
            }
            return cell;
        }
    }
    
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 7;
}
//cell高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //    if (indexPath.section == 1) {
    if (indexPath.row == 1) {
        if ([[_selectArray objectAtIndex:indexPath.row] isEqualToString:@"1"]) {
            return 50;
        }
        else
        {
            return 300;
        }
    }
    else if (indexPath.row == 3){
        if ([[_selectArray objectAtIndex:indexPath.row] isEqualToString:@"1"]) {
            return 50 *kscale;
        }
        else
        {
                float sum = 0;
                UILabel *lab = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, (kScreenW -30)*kscale, 12)];
                [lab setNumberOfLines:0];
                [lab setFont:[UIFont systemFontOfSize:12]];
            for (int i = 0 ; i<_infoArray.count; i++) {
                lab.frame = CGRectMake(0, 0, (kScreenW -30)*kscale, 0);
                [lab setText:[NSString stringWithFormat:@"%@",[_infoArray objectAtIndex:i]]];
                [lab sizeToFit];
                sum = sum +lab.frame.size.height;
            }
            NSLog(@"%f",sum);
            return (3*34 +sum +65) *kscale ;
        }
        
    }
    else if (indexPath.row == 5)
    {
        if ([[_selectArray objectAtIndex:indexPath.row] isEqualToString:@"1"]) {
            return 50;
        }
        else
        {
            return ((self.dataSource.count *67) + 39 +100) *kscale;
        }
    }
    else
    {
        if (indexPath.row == 6) {
            return 1;
        }
        else
        {
            return 10;
        }
    }
    
    
    return 50;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    
    
}
-(UIView *)lineView
{
    UIView *bgView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenW, 10)];
    [bgView setBackgroundColor:[UIColor grayColor]];
    UILabel *line = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, kScreenW, 0.5)];
    [line setBackgroundColor:[UIColor blackColor]];
    [bgView addSubview:line];
    UILabel *line1 = [[UILabel alloc] initWithFrame:CGRectMake(0, 9.5, kScreenW, 0.5)];
    [line1 setBackgroundColor:[UIColor grayColor]];
    [bgView addSubview:line1];
    return bgView;
}
//
- (UIView *)setTopView
{
    UIView *topView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenW, (482-64)*kscale)];
    UIView *topOne = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenW, (245-64)*kscale)];
    UIImageView *oneBackImg = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"jianbian"]];
    oneBackImg.frame = topOne.frame;
    [topOne addSubview:oneBackImg];
    [topView addSubview:topOne];
    _numberLab = [self createLabelWithFrame:CGRectMake(0, 0*kscale, kScreenW, 15) text:@"双十一备货周转FC16122601" textFont:[UIFont systemFontOfSize:12] textColor:TEXTCOLOR];
    _numberLab.text = self.model.postNumber;
    [topOne addSubview:_numberLab];
    
    _persentLab = [self createLabelWithFrame:CGRectMake(0, 32*kscale, kScreenW, 36*kscale) text:@"8.6%" textFont:[UIFont systemFontOfSize:34] textColor:[UIColor colorWithRed:146.0/255.0 green:99.0/255.0 blue:.0/255.0 alpha:1]];
    _persentLab.text = [NSString stringWithFormat:@"%.2f%%",[self.model.rate floatValue]/100];
    _persentLab.attributedText = [self changeLabelWithText:_persentLab.text];
    [topOne addSubview:_persentLab];
    
    _expectLab = [self createLabelWithFrame:CGRectMake(0, 77*kscale, kScreenW, 15*kscale) text:@"预期年化" textFont:[UIFont systemFontOfSize:12] textColor:[UIColor colorWithRed:64.0/255.0 green:46.0/255.0 blue:6.0/255.0 alpha:1]];
    [topOne addSubview:_expectLab];
    
    //三条线
    UILabel *lineLab = [self createLabelWithFrame:CGRectMake(0, (183-64)*kscale, kScreenW, 1) text:nil textFont:nil textColor:[UIColor colorWithRed:218.0/255.0 green:209.0/255.0 blue:179.0/255.0 alpha:1]];
    lineLab.backgroundColor = [UIColor colorWithRed:218.0/255.0 green:209.0/255.0 blue:179.0/255.0 alpha:0.5];
    [topOne addSubview:lineLab];
    
    UILabel *shuLineLab = [self createLabelWithFrame:CGRectMake(kScreenW/2, (193-64)*kscale, 1, 44*kscale) text:nil textFont:nil textColor:[UIColor colorWithRed:218.0/255.0 green:209.0/255.0 blue:179.0/255.0 alpha:0.5]];
    shuLineLab.backgroundColor = [UIColor colorWithRed:218.0/255.0 green:209.0/255.0 blue:179.0/255.0 alpha:1];
    [topOne addSubview:shuLineLab];
    UILabel *hengLineLab = [self createLabelWithFrame:CGRectMake(0, 181*kscale, kScreenW , 0.5) text:nil textFont:nil textColor:[UIColor colorWithRed:218.0/255.0 green:209.0/255.0 blue:179.0/255.0 alpha:0.5]];
    hengLineLab.backgroundColor = [UIColor colorWithRed:218.0/255.0 green:209.0/255.0 blue:179.0/255.0 alpha:1];
    [topOne addSubview:hengLineLab];
    
    _monthLab = [self createLabelWithFrame:CGRectMake(0, 132*kscale, kScreenW/2, 20) text:@"3个月" textFont:[UIFont systemFontOfSize:16] textColor:[UIColor colorWithRed:159.0/255.0 green:117.0/255.0 blue:20.0/255.0 alpha:1]];
    if ([self.model.periodType isEqualToString:@"月"]) {
        _monthLab.text = [NSString stringWithFormat:@"%@个月",self.model.period];
    }
    else
    {
        _monthLab.text = [NSString stringWithFormat:@"%@天",self.model.period];
    }
    [topOne addSubview:_monthLab];
    _limitLab = [self createLabelWithFrame:CGRectMake(0, 155*kscale, kScreenW/2, 20) text:@"封闭期限" textFont:[UIFont systemFontOfSize:14] textColor:TEXTCOLOR];
    [topOne addSubview:_limitLab];
    
    _moneyLab = [self createLabelWithFrame:CGRectMake(kScreenW/2, 132*kscale, kScreenW/2, 20) text:@"156,000.00元" textFont:[UIFont systemFontOfSize:16] textColor:[UIColor colorWithRed:159.0/255.0 green:117.0/255.0 blue:20.0/255.0 alpha:1]];
    _moneyLab.text = [ZHRTBasicMethod ChangeNumberFormat:self.model.remainMoney];
    [topOne addSubview:_moneyLab];
    
    _leftLab = [self createLabelWithFrame:CGRectMake(kScreenW/2, 155*kscale, kScreenW/2, 20) text:@"剩余可投金额" textFont:[UIFont systemFontOfSize:14] textColor:TEXTCOLOR];
    [topOne addSubview:_leftLab];
    
    UIImageView *processImg = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"jindu"]];
    processImg.frame = CGRectMake(30*kscale, 193*kscale, kScreenW-60*kscale, 25*kscale);
    [topView addSubview:processImg];
    
    _dateOneLab = [self createLabelWithFrame:CGRectMake(15*kscale, 226*kscale, 150*kscale, 12) text:@"2017-02-02" textFont:[UIFont systemFontOfSize:10] textColor:[UIColor colorWithRed:146.0/255.0 green:92.0/255.0 blue:.0/255.0 alpha:1]];
    _dateOneLab.textAlignment = NSTextAlignmentLeft;
    [topView addSubview:_dateOneLab];
    _dateTwoLab = [self createLabelWithFrame:CGRectMake((kScreenW-150*kscale)/2, 226*kscale, 150*kscale, 12) text:@"2017-05-02" textFont:[UIFont systemFontOfSize:10] textColor:[UIColor colorWithRed:146.0/255.0 green:92.0/255.0 blue:.0/255.0 alpha:1]];
    [topView addSubview:_dateTwoLab];
    _dateThreeLab = [self createLabelWithFrame:CGRectMake(kScreenW-165*kscale, 226*kscale, 150*kscale, 12) text:@"2017-05-02" textFont:[UIFont systemFontOfSize:10] textColor:[UIColor colorWithRed:146.0/255.0 green:92.0/255.0 blue:.0/255.0 alpha:1]];
    _dateThreeLab.textAlignment = NSTextAlignmentRight;
    [topView addSubview:_dateThreeLab];
    
    //矩形及内部控件
    UIImageView * squareImg = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"planimage"]];
    [topView addSubview:squareImg];
    squareImg.frame = CGRectMake(15, 250*kscale, kScreenW - 30, 110*kscale);
    UILabel *xiangmuLab = [self createLabelWithFrame:CGRectMake(15*kscale, 19*kscale, 80, 16) text:@"项目总额" textFont:[UIFont systemFontOfSize:14] textColor:[UIColor colorWithRed:153.0/255.0 green:153.0/255.0 blue:153.0/255.0 alpha:1]];
    [squareImg addSubview:xiangmuLab];
    _totalLab = [self createLabelWithFrame:CGRectMake(100*kscale, 19*kscale, 150, 16) text:@"200,000,00元" textFont:[UIFont systemFontOfSize:14] textColor:TEXTCOLOR];
    _totalLab.textAlignment = NSTextAlignmentLeft;
    [squareImg addSubview:_totalLab];
    
    
    UILabel *fangshiLab = [self createLabelWithFrame:CGRectMake(15*kscale, 48*kscale, 80, 16) text:@"还款方式" textFont:[UIFont systemFontOfSize:14] textColor:[UIColor colorWithRed:153.0/255.0 green:153.0/255.0 blue:153.0/255.0 alpha:1]];
    [squareImg addSubview:fangshiLab];
    _payBackLab = [self createLabelWithFrame:CGRectMake(100*kscale, 48*kscale, 200, 16) text:@"每月还息，到期还本" textFont:[UIFont systemFontOfSize:14] textColor:TEXTCOLOR];
    _payBackLab.textAlignment = NSTextAlignmentLeft;
    [squareImg addSubview:_payBackLab];
    
    
    UILabel *jigouLab = [self createLabelWithFrame:CGRectMake(15*kscale, 77*kscale, 80, 16) text:@"担保机构" textFont:[UIFont systemFontOfSize:14] textColor:[UIColor colorWithRed:153.0/255.0 green:153.0/255.0 blue:153.0/255.0 alpha:1]];
    [squareImg addSubview:jigouLab];
    _companyLab = [self createLabelWithFrame:CGRectMake(100*kscale, 77*kscale, 200, 16) text:@"北京恒远担保有限公司" textFont:[UIFont systemFontOfSize:14] textColor:TEXTCOLOR];
    _companyLab.textAlignment = NSTextAlignmentLeft;
    [squareImg addSubview:_companyLab];
    
    
    return topView;
}
#pragma mark ---点击方法
//右Item被点击
- (void)rightItemClick
{
    
}
- (void)letfBarClick
{
    [self.navigationController popViewControllerAnimated:YES];
}
//点击计算器按钮
- (void)calculatorBtnClick
{
    _maskView = [[UIView alloc] initWithFrame:kScreenB];
    _maskView.backgroundColor = [UIColor colorWithRed:0/255.0 green:0/255.0 blue:0/255.0 alpha:0.3];
    [self.view addSubview:_maskView];
    
    _calculatorBackView = [[UIView alloc] initWithFrame:CGRectMake(50, kScreenH, kScreenW - 100, 192*kscale)];
    _calculatorBackView.layer.cornerRadius = 10;
    _calculatorBackView.backgroundColor = [UIColor whiteColor];
    [_maskView addSubview:_calculatorBackView];
    [UIView animateWithDuration:0.25 animations:^{
        _calculatorBackView.frame = CGRectMake(50, 66, kScreenW - 100, 192*kscale);;
    }];
    
    UILabel *calTitleLab = [self createLabelWithFrame:CGRectMake((_calculatorBackView.frame.size.width-150)/2, 16*kscale, 150, 16*kscale) text:@"收益计算器" textFont:[UIFont systemFontOfSize:14] textColor:[UIColor colorWithRed:51.0/255.0 green:51.0/255.0 blue:51.0/255.0 alpha:1]];
    [_calculatorBackView addSubview:calTitleLab];
    
    UIButton *dismissBtn = [[UIButton alloc] initWithFrame:CGRectMake(_calculatorBackView.frame.size.width-10-12*kscale, 17, 12*kscale, 12*kscale)];
    [dismissBtn setBackgroundImage:[UIImage imageNamed:@"Plus"] forState:UIControlStateNormal];
    [dismissBtn addTarget:self action:@selector(dismissBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [_calculatorBackView addSubview:dismissBtn];
    
    UIImageView *calTFbackView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"textfield"]];
    calTFbackView.frame = CGRectMake(10, 48*kscale, _calculatorBackView.frame.size.width - 20, 40*kscale);
    calTFbackView.userInteractionEnabled = YES;
    [_calculatorBackView addSubview:calTFbackView];
    _calTF = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, calTFbackView.frame.size.width, calTFbackView.frame.size.height)];
    _calTF.placeholder = @"100元起投";
    _calTF.keyboardType = UIKeyboardTypeNumberPad;
    [_calTF addTarget:self action:@selector(calTFChange) forControlEvents:UIControlEventEditingChanged];
    [calTFbackView addSubview:_calTF];
   
    _newIncomeLab = [self createLabelWithFrame:CGRectMake((_calculatorBackView.frame.size.width-150)/2, 101*kscale, 150, 18*kscale) text:@"0.00元" textFont:[UIFont systemFontOfSize:16] textColor:TEXTCOLOR];
    [_calculatorBackView addSubview:_newIncomeLab];
    
    UILabel *yuLab = [self createLabelWithFrame:CGRectMake((_calculatorBackView.frame.size.width-100)/2, 120*kscale, 100, 16*kscale) text:@"预期收益" textFont:[UIFont systemFontOfSize:12] textColor:[UIColor colorWithRed:102.0/255.0 green:102.0/255.0 blue:102.0/255.0 alpha:1]];
    [_calculatorBackView addSubview:yuLab];
    
    _calPercentLab = [self createLabelWithFrame:CGRectMake(0, 142*kscale, _calculatorBackView.frame.size.width/2, 18) text:@"12.00%" textFont:[UIFont systemFontOfSize:16] textColor:[UIColor colorWithRed:187.0/255.0 green:147.0/255.0 blue:155.0/255.0 alpha:1]];
    _calPercentLab.text = [NSString stringWithFormat:@"%.2f%%",[self.model.rate floatValue]];
    [_calculatorBackView addSubview:_calPercentLab];
    
    UILabel *yearLab = [self createLabelWithFrame:CGRectMake(0, 160*kscale, _calculatorBackView.frame.size.width/2, 18) text:@"预期年化" textFont:[UIFont systemFontOfSize:12] textColor:TEXTCOLOR];
    [_calculatorBackView addSubview:yearLab];
    
    _calMonthLab = [self createLabelWithFrame:CGRectMake(_calculatorBackView.frame.size.width/2, 142*kscale, _calculatorBackView.frame.size.width/2, 18) text:@"12个月" textFont:[UIFont systemFontOfSize:16] textColor:[UIColor colorWithRed:187.0/255.0 green:147.0/255.0 blue:155.0/255.0 alpha:1]];
    if ([self.model.periodType isEqualToString:@"月"]) {
        _calMonthLab.text = [NSString stringWithFormat:@"%@个月",self.model.period];
    }
    else
    {
        _calMonthLab.text = [NSString stringWithFormat:@"%@天",self.model.period];
    }

    [_calculatorBackView addSubview:_calMonthLab];
    
    UILabel *fengLab = [self createLabelWithFrame:CGRectMake(_calculatorBackView.frame.size.width/2, 160*kscale, _calculatorBackView.frame.size.width/2, 18) text:@"封闭期限" textFont:[UIFont systemFontOfSize:12] textColor:TEXTCOLOR];
    [_calculatorBackView addSubview:fengLab];
    
    UILabel *lineLable = [[UILabel alloc] init];
    lineLable.frame = CGRectMake(_calculatorBackView.frame.size.width/2, 142*kscale, 1, 35*kscale);
    lineLable.backgroundColor = [UIColor colorWithRed:231.0/255.0 green:231.0/255.0 blue:231.0/255.0 alpha:1];
    [_calculatorBackView addSubview:lineLable];
    
//    NSDate *today = [NSDate date];
//    NSCalendar *c = [NSCalendar currentCalendar];
//    NSRange days = [c rangeOfUnit:NSCalendarUnitDay inUnit:NSCalendarUnitMonth forDate:today];
//    [self getDateInfo];
}
- (void) getDateInfo
{
    NSDate * date  = [NSDate date];
    NSCalendar * calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian]; // 指定日历的算法 NSCalendarIdentifierGregorian,NSGregorianCalendar
    // NSDateComponent 可以获得日期的详细信息，即日期的组成
    NSDateComponents *comps = [calendar components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay|NSCalendarUnitHour|NSCalendarUnitHour|NSCalendarUnitSecond|NSCalendarUnitWeekday fromDate:date];
    
    NSLog(@"年 = year = %ld",comps.year);
    NSLog(@"月 = month = %ld",comps.month);
    NSLog(@"日 = day = %ld",comps.day);
    NSLog(@"时 = hour = %ld",comps.hour);
    NSLog(@"分 = minute = %ld",comps.minute);
    NSLog(@"秒 = second = %ld",comps.second);
    NSLog(@"星期 =weekDay = %ld ",comps.weekday);
    
}
- (NSInteger)getNumberOfDaysInMonth
{
    NSCalendar * calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian]; // 指定日历的算法
    NSDate * currentDate = [NSDate date];
    // 只要个时间给日历,就会帮你计算出来。这里的时间取当前的时间。
    NSRange range = [calendar rangeOfUnit:NSCalendarUnitDay
                                   inUnit: NSCalendarUnitMonth
                                  forDate:currentDate];
    return range.length;
}
- (void)dismissBtnClick
{
    [_maskView removeFromSuperview];
}
//点击立即抢购
- (void)startBuyClick
{
    XDLMImmediateInvestmentViewController *XDLMImmediateInvestmentView = [[XDLMImmediateInvestmentViewController alloc] init];
    [self.navigationController pushViewController:XDLMImmediateInvestmentView animated:YES];
}
//计算器输入框值变化的监听
- (void)calTFChange
{
    if ([self.model.periodType isEqualToString:@"月"]) {
        _newIncomeLab.text = [self makeInterestWithPrincipal:[_calTF.text floatValue] Month:[self.model.period integerValue] andInterestRate:[self.model.rate floatValue]/100];
    }
    else
    {
        
    }
    
}
/**
 计算利息
 
 @param principal 本金
 @param month     存入月份
 @param rate      利率
 
 @return 利息
 */
-(NSString *)makeInterestWithPrincipal:(CGFloat)principal  Month:(NSInteger)month andInterestRate:(CGFloat)rate{
    NSString *interest;
    
    NSDate * mydate = [NSDate date];
    NSDateFormatter * dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    NSDateComponents *comps = nil;
    comps = [calendar components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitMonth fromDate:mydate];
    NSDateComponents *adcomps = [[NSDateComponents alloc] init];
    [adcomps setYear:0];
    [adcomps setMonth:month];
    [adcomps setDay:0];
    NSDate *newdate = [calendar dateByAddingComponents:adcomps toDate:mydate options:0];
    long long time = newdate.timeIntervalSince1970 - mydate.timeIntervalSince1970;
    int betweenDay = (int)(time / (60*60*24));
    
    interest = [NSString stringWithFormat:@"%.2f",principal * rate / 365 * betweenDay];
    
    return interest;
}
#pragma mark ---辅助放方法
//改变Label.Text后几位字符串大小
-(NSMutableAttributedString*) changeLabelWithText:(NSString*)needText
{
    
    NSMutableAttributedString *strAttribute = [[NSMutableAttributedString alloc] initWithString:needText];
    [strAttribute addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:16] range:NSMakeRange(needText.length - 1, 1)];
    return strAttribute;
}

- (UILabel *)createLabelWithFrame:(CGRect)frame text:(NSString *)text textFont:(UIFont *)font textColor:(UIColor *)color {
    
    UILabel * label = [[UILabel alloc] initWithFrame:frame];
    label.text = text;
    label.textColor = color;
    label.font = font;
    label.adjustsFontSizeToFitWidth = YES;
    label.textAlignment = NSTextAlignmentCenter;
    
    return label;
    
}
//创建navigationItem
- (void)createRightItem
{
    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithTitle:@"安全须知" style:UIBarButtonItemStylePlain target:self action:@selector(rightItemClick)];
    rightItem.tintColor = [UIColor grayColor];
    
    self.navigationItem.rightBarButtonItem = rightItem;
}
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden = NO;
    self.navigationController.navigationBar.translucent = false;
    //    self.navigationItem.titleView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"navtitlelogo"]];
    self.navigationController.navigationBar.barStyle = UIBarStyleBlackTranslucent;
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"juxing"] forBarMetrics:UIBarMetricsDefault];
    self.navigationItem.leftBarButtonItem = [XDLMUIFactory createBBIWithImageStr:@"backblack" target:self action:@selector(letfBarClick)];
}
#pragma mark --Getter
-(NSMutableArray *)problemArray
{
    if (_problemArray == nil) {
        _problemArray = [[NSMutableArray alloc] init];
    }
    return _problemArray;
}
-(NSMutableArray *)infoArray
{
    if (_infoArray == nil) {
        _infoArray = [[NSMutableArray alloc] init];
    }
    return _infoArray;

}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
